<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="24">
            <a-form-model-item label="联系人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lxr">
              <a-input :disabled="formDisabled" v-model="model.lxr" placeholder="请输入联系人"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="部门/职务" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="departOrPost">
              <a-input :maxLength="200" :disabled="formDisabled" v-model="model.departOrPost" placeholder="部门/职务"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lxfs">
              <a-input :disabled="formDisabled" v-model="model.lxfs" placeholder="请输入联系电话"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  export default {
    name: 'ErpOmcCustomerManagerContactForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      var validatorPhone = async (rule,value,callback)=>{
        if (!value) {
            callback(new Error('请输入联系电话'));
        } else {
            if (value == '/') {
              callback();
            } else if (value != '') {
                let reg = /^1\d{10}$|^(0\d{2,3}-?|\(0\d{2,3}\))?[1-9]\d{4,7}(-\d{1,8})?$/
                if (!reg.test(value)) {
                    callback(new Error('请输入正确的号码'));
                }
            }
            callback();
        }
      };
      return {
        flag: false,
        model:{
         },
        labelCol: {
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 },
        },
        confirmLoading: false,
        validatorRules: {
          lxr: [
            { required: true, message: '请输入联系人!', trigger: 'blur'},
          ],
          lxfs: [
              { required: true, message: '请输入联系电话!', trigger: 'blur'},
              { validator: validatorPhone, trigger: "blur" },
          ]
        },
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.$refs["form"].clearValidate();
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.$emit('ok', this.model, this.flag);
          }
        })
      },
    }
  }
</script>
